﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Forms;
using System.Windows.Forms.VisualStyles;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Ecng.Common;
using Ecng.Xaml;
using Sample.TradingClasses;
using Sample.TradingClasses.RobotsData;
using StockSharp.Algo.Storages;
using StockSharp.BusinessEntities;
using Sample.Properties;


namespace Sample.StartTrading
{
    /// <summary>
    /// Логика взаимодействия для CreateRobot.xaml
    /// </summary>
    public partial class CreateRobot : Window
    {

 //       private readonly OpenRobot _openRobot = new OpenRobot("");

        public CreateRobot()
        {
            InitializeComponent();
            //OpenRobot.
           // _openRobot.MakeHideable();

            XmlFileFolder.Content = Settings.Default.XmlFolder;
            //XmlFileFolder.Text = Settings.Default.XmlFolder;
            RobotName.Text = Settings.Default.RobotNameStart;

            infoXMLfile = new ssXmlStorage(RobotName.Text); // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
            _setStore = new SettingsStorage();

        }

        //public interface IRobotStartXmlInfo
        //{
        //    DateTime RobotDateTimeCreating { get; set; }
        //    RobotType RobotType { get; set; }
        //    string RobotDirectoryNameDB { get; set; }
        //    string RobotFileNameDB { get; set; }
        //    Portfolio RobotPF { get; set; }
        //}

        private ssXmlStorage infoXMLfile;// = new ssXmlStorage("");
        public string FullNameFileXML;
        public string FullNameFileDB;
        public string Settingspath;
        public string CheckString;
        private SettingsStorage _setStore;


        private void CreateNewRobot_Click(object sender, RoutedEventArgs e)
        {
            string addName = "";
            string toShow;
            string xmlStartFileName = RobotName.Text;

            if (AutomatRobot.IsChecked != false) {addName = "Auto_";}
            if (BiasRobot.IsChecked != false) { addName = "Bias_"; }
            if (ManualRobot.IsChecked != false) { addName = "Manual_"; }

            FullNameFileXML = XmlFileFolder.Content.ToString() + "\\" + addName + xmlStartFileName + ".xml";
            FullNameFileDB = XmlFileFolder.Content.ToString() + "\\" +  addName + xmlStartFileName + ".dbf";

            if (!File.Exists(FullNameFileXML))
            {
 
                infoXMLfile = new ssXmlStorage(FullNameFileXML);
                infoXMLfile.Stroka1 = FullNameFileXML;
                infoXMLfile.RobotFileNameXML = xmlStartFileName + ".xml";
                infoXMLfile.RobotFolderNameXML = XmlFileFolder.Content.ToString();
                infoXMLfile.RobotFullNameXML = FullNameFileXML;
                infoXMLfile.RobotFullNameDB = FullNameFileDB;
                infoXMLfile.RobotDateTimeCreating = DateTime.Now;
                infoXMLfile.RobotName = RobotName.Text;
                infoXMLfile.RobotMode = ONOFF.OFF;
      //          infoXMLfile.RobotType = RobotName.Text;

                if (AutomatRobot.IsChecked != false)
                {
                    infoXMLfile.RobotType = RobotType.Auto;
                }
                else
                {
                    if (BiasRobot.IsChecked != false)
                    {
                        infoXMLfile.RobotType = RobotType.Bias;
                    }
                    else
                    {
                        if (ManualRobot.IsChecked != false)
                    {
                        infoXMLfile.RobotType = RobotType.Manual;
                    }
      
                    }
                }

                infoXMLfile.Save(_setStore);

                CreateNewRobot.IsEnabled = false;
                RunNewRobot.IsEnabled = true;


            }
            else
            {
                toShow = "XML-File: " + FullNameFileXML + " exists. Choose another name !";
                System.Windows.Forms.MessageBox.Show(toShow);
            }
            //infoXMLfile = new ssXmlStorage(FullNameFileXML);

            //qqq.RobotDateTimeCreating =  DateTime;
            //qqq.RobotFileNameDB = RobotName.Text;
            //qqq.RobotFolderNameDB = XmlFileFolder.Text;

        }

        /*
         
        private void CreateNewRobot_Click(object sender, RoutedEventArgs e)
        {
            string addName = "";
            string toShow;
            string xmlStartFileName = RobotName.Text;

            if (AutomatRobot.IsChecked != false) {addName = "Auto_";}
            if (BiasRobot.IsChecked != false) { addName = "Bias_"; }
            if (ManualRobot.IsChecked != false) { addName = "Manual_"; }

            FullNameFileXML = XmlFileFolder.Content.ToString() + addName + xmlStartFileName + ".xml";
            FullNameFileDB = XmlFileFolder.Content.ToString() + addName + xmlStartFileName + ".dbf";

            if (!File.Exists(FullNameFileXML))
            {
 
                infoXMLfile = new ssXmlStorage(FullNameFileXML);
                infoXMLfile.Stroka1 = FullNameFileXML;
                infoXMLfile.RobotFileNameXML = xmlStartFileName + ".xml";
                infoXMLfile.RobotFolderNameXML = XmlFileFolder.Content.ToString();
                infoXMLfile.RobotFullNameXML = FullNameFileXML;
                infoXMLfile.RobotFullNameDB = FullNameFileDB;
                infoXMLfile.RobotDateTimeCreating = DateTime.Now;
                infoXMLfile.RobotName = RobotName.Text;

                if (AutomatRobot.IsChecked != null)
                {
                    infoXMLfile.RobotType = RobotType.Auto;
                    addName = "Auto";
                }
                else
                {
                    if (BiasRobot.IsChecked != null)
                    {
                        infoXMLfile.RobotType = RobotType.Bias;
                        addName = "Bias";
                    }
                    else
                    {             
                        if (ManualRobot.IsChecked != null)
                    {
                        infoXMLfile.RobotType = RobotType.Manual;
                        addName = "Manual";
                    }
      
                    }
                }

                infoXMLfile.Save(_setStore);

                CreateNewRobot.IsEnabled = false;
                RunNewRobot.IsEnabled = true;


            }
            else
            {
                toShow = "XML-File: " + FullNameFileXML + " exists. Choose another name !";
                System.Windows.Forms.MessageBox.Show(toShow);
            }
            //infoXMLfile = new ssXmlStorage(FullNameFileXML);

            //qqq.RobotDateTimeCreating =  DateTime;
            //qqq.RobotFileNameDB = RobotName.Text;
            //qqq.RobotFolderNameDB = XmlFileFolder.Text;


        }

         */



        public void RunNewRobot_Click(object sender, RoutedEventArgs e)
        {
            OpenRobot _openRobot = new OpenRobot(infoXMLfile.RobotFullNameXML);

            MainWindow.ShowOrHide(_openRobot);


            //MainWindow.ShowOrHide(_openRobot(infoXMLfile.RobotFullNameXML));
      //      RoboViewerBasic._createRobot.Close();

        }


        private void FindFolder_Click(object sender, RoutedEventArgs e)
        {
			var dialog = new FolderBrowserDialog();

            if (!XmlFileFolder.Content.IsNull())
                dialog.SelectedPath = XmlFileFolder.Content.ToString();

            if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                XmlFileFolder.Content = dialog.SelectedPath;
            }

           Settings.Default.XmlFolder =  XmlFileFolder.Content.ToString();
        }

        private void TypeOfRobot_Checked(object sender, RoutedEventArgs e)
        {
            if (RobotName.Text.IsNormalized() && RobotName.Text.Length > 0) 
            CreateNewRobot.IsEnabled = true;
        }

        
        protected override void OnClosing(CancelEventArgs e)
        {
           Settings.Default.RobotNameStart = RobotName.Text;
            Settings.Default.XmlFolder = XmlFileFolder.Content.ToString();
           Settings.Default.Save();
            base.OnClosed(e);// .OnClosing(e);
        }

    }
}
